using Framework;
using UnityEngine;


namespace Game
{
    public class FightManager : FrameworkModule
    {
        public bool IsInFight { get; private set; }

        public override bool NeedUpdate => true;

        RoleWarriorEntity m_MainRole;  // 主角

        // 进入战斗场景
        public async FTask InitFight()
        {
            this.m_MainRole = new RoleWarriorEntity();
            await this.m_MainRole.RefreshCharacter("YBot");
        }

        // 加载界面关闭，正式进入战斗
        public void StartFight()
        {
            this.IsInFight = true;
            this.AddEvents();
        }

        // 离开战斗场景
        public void LeaveFight()
        {
            this.DelEvents();
            this.IsInFight = false;
        }

        // 暂停战斗
        public void PauseFight()
        {
            Log.Info("[Fight] PauseFight");
            GameEntry.Event.BroadCast(GlobalEventType.PauseFight);
            this.m_MainRole.PauseFight();
            // GameEntry.UI.OpenUI(AssetUtility.GetUIPath("Fight/FightPausePanel")).Coroutine();
        }

        // 恢复战斗
        public void ResumeFight()
        {
            Log.Info("[Fight] ResumeFight");
            this.m_MainRole.ResumeFight();
        }

        private void AddEvents()
        {
            GameEntry.Event.AddListener(GlobalEventType.ResumeFight, this.ResumeFight);
            GameEntry.Input.AddKeyCodeListener(KeyCode.Escape, this.PauseFight);
        }

        private void DelEvents()
        {
            GameEntry.Event.DelListener(GlobalEventType.ResumeFight, this.ResumeFight);
            GameEntry.Input.DelKeyCodeListener(KeyCode.Escape, this.PauseFight);
        }

        public override void Update(float elapseSeconds, float realElapseSeconds)
        {
            if (!this.IsInFight) { return; }
            this.m_MainRole.Update(elapseSeconds, realElapseSeconds);
        }
    }
}